package com.ruoyi.project.business.dailyschedulerule.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.project.business.dailyschedulerule.entity.DailyscheduleRule;
import com.ruoyi.project.business.dailyschedulerule.mapper.DailyscheduleRuleMapper;
import com.ruoyi.project.business.dailyschedulerule.request.AddDailyscheduleRuleRequest;
import com.ruoyi.project.business.dailyschedulerule.request.ListDailyscheduleRuleRequest;
import com.ruoyi.project.business.dailyschedulerule.response.DailyscheduleRuleResponse;
import com.ruoyi.project.business.dailyschedulerule.service.IDailyscheduleRuleService;
import com.ruoyi.project.business.domain.rules.Rule;
import com.ruoyi.project.business.domain.rules.daily.ExpectedTime;
import com.ruoyi.project.business.domain.utils.RuleUtil;
import com.ruoyi.project.business.utils.BusinessUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 日排班规则Service业务层处理
 * 
 * @author ${author}
 * @date 2024-04-03
 */
@Service
public class DailyscheduleRuleServiceImpl implements IDailyscheduleRuleService {
    @Autowired
    private DailyscheduleRuleMapper dailyscheduleRuleMapper;

    /**
     * 查询日排班规则
     * 
     * @return 日排班规则
     */
    @Override
    public DailyscheduleRuleResponse selectDailyscheduleRuleById(Long id) {
        DailyscheduleRule dailyscheduleRule = dailyscheduleRuleMapper.selectDailyscheduleRuleById(id);
        if(dailyscheduleRule == null) {
            return null;
        }
        DailyscheduleRuleResponse copy = BeanUtils.copy(dailyscheduleRule, DailyscheduleRuleResponse.class);
        BusinessUtil.fillSingleNames(copy, dailyscheduleRule.getTenantId());
        return copy;
    }

    /**
     * 查询日排班规则列表
     * 
     * @return 日排班规则
     */
    @Override
    public List<DailyscheduleRuleResponse> selectDailyscheduleRuleList(ListDailyscheduleRuleRequest request, Long tenantId) {
        // TODO request to  param
        DailyscheduleRule param = BeanUtils.copy(request, DailyscheduleRule.class);
        param.setTenantId(tenantId);
        List<DailyscheduleRule> entitis = dailyscheduleRuleMapper.selectDailyscheduleRuleList(param);
        List<DailyscheduleRuleResponse> resList = BeanUtils.copyList(entitis, DailyscheduleRuleResponse.class);

        BusinessUtil.fillListNames(resList, tenantId);
        return resList;
    }

    /**
     * 新增日排班规则
     * 
     * @return 结果
     */
    @Override
    public int insertDailyscheduleRule(AddDailyscheduleRuleRequest request, Long operatorId, Long tenantId) {
        DailyscheduleRule entity = BeanUtils.copy(request, DailyscheduleRule.class);
        entity.setTenantId(tenantId);
        entity.setCreator(operatorId);
        entity.setDeleted(Boolean.FALSE);
        entity.setCreateTime(DateUtils.getNowDate());
        return dailyscheduleRuleMapper.insertDailyscheduleRule(entity);
    }

    /**
     * 修改日排班规则
     * 
     * @return 结果
     */
    @Override
    public int updateDailyscheduleRule(DailyscheduleRule entity, Long operatorId, Long tenantId) {
        entity.setUpdateTime(DateUtils.getNowDate());
        entity.setTenantId(tenantId);
        entity.setUpdater(operatorId);
        return dailyscheduleRuleMapper.updateDailyscheduleRule(entity);
    }

    /**
     * 批量删除日排班规则
     * 
     * @return 结果
     */
    @Override
    public int deleteDailyscheduleRuleByIds(Long[] ids, Long operatorId, Long tenantId) {
        return dailyscheduleRuleMapper.deleteDailyscheduleRuleByIds(ids);
    }

    /**
     * 删除日排班规则信息
     * 
     * @return 结果
     */
    @Override
    public int deleteDailyscheduleRuleById(Long id, Long operatorId, Long tenantId) {
        return dailyscheduleRuleMapper.deleteDailyscheduleRuleById(id);
    }

    @Override
    public List<DailyscheduleRule> selectRulesByUserIds(List<Long> userIds, LocalDate assignDate, Long tenantId) {
        return dailyscheduleRuleMapper.selectRulesByUserIds(userIds, assignDate, tenantId);
    }

    @Override
    public Map<Long, List<Rule>> selectRulesMapByUserIds(List<Long> userIds, LocalDate assignDate, Long tenantId) {
        List<DailyscheduleRule> dbRules = this.selectRulesByUserIds(userIds, assignDate, tenantId);
        if(CollectionUtils.isNotEmpty(dbRules)) {
            Map<Long, List<Rule>> collect = dbRules.stream().map(rule -> {
                Rule tmp = RuleUtil.getRule(rule);
                return tmp;
            }).collect(Collectors.groupingBy(Rule::getUserId));
            return collect;
        }
        return new HashMap<>();
    }

}
